package cc.linker.steplink.crm.merchant.controller;

import cc.linker.steplink.crm.commons.coupon.CouponCustomerInfo;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchant.common.PcCouponCustomer;
import cc.linker.steplink.crm.merchanth5.pojo.RegisterActivityPo;
import cc.linker.steplink.crm.merchanth5.service.RegisterActivityService;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: CKQ
 * @DateTime: 2020/12/17 16:56
 * @Description: TODO
 */
@RestController
@RequestMapping("/registerActivity")
public class RegisterActivityController {

    @Autowired
    private RegisterActivityService registerActivityService;

    @Autowired
    private UserInfoUtil userInfoUtil;
    @Autowired
    private PcCouponCustomer pcCouponCustomer;


    /**
     * @param activityName 活动名称
     * @param status       状态 -1 全部 0未开始 1已开始 2已结束 3已暂停
     * @param pageIndex    当前第几页
     * @param pageSize     每页多少条
     * @Description：分页列表
     * @DateTime：2020/12/17 17:00
     * @Params：
     * @Return：
     */
    @GetMapping("/pageList")
    public PageInfo<RegisterActivityPo> getRegisterActivityListByOrgId(@RequestParam(value = "activityName", required = false, defaultValue = "") String activityName,
                                                                       @RequestParam(value = "status", defaultValue = "-1") Integer status,
                                                                       @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                                                                       @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                                                       HttpServletRequest request) {
        PcUserInfo userInfo = this.userInfoUtil.getPcUserInfo(request);
        return this.registerActivityService.getRegisterActivityListByOrgId(activityName, status, pageIndex, pageSize, userInfo.getOrgId());
    }

    /**
    * @Description：获取注册有礼活动信息
    * @DateTime：2020/12/18 11:28
    * @Params：
     * @param activityId 活动ID
    * @Return：
    */
    @GetMapping("/info")
    public  RegisterActivityPo getRegisterActivityInfo(@RequestParam(value = "activityId", required = false, defaultValue = "") String activityId,
                                                       HttpServletRequest request){
        PcUserInfo userInfo = this.userInfoUtil.getPcUserInfo(request);
        CouponCustomerInfo couponCustomerInfo = pcCouponCustomer.getCouponCustomerInfo(request);

        return  this.registerActivityService.getRegisterActivityInfo(activityId,couponCustomerInfo);
    }

    /**
     * @param registerActivityPo 活动实体
     * @Description：添加注册活动
     * @DateTime：2020/12/17 17:18
     * @Params：
     * @Return：
     */
    @PostMapping("/insert")
    public void insertRegisterActivity(@RequestBody RegisterActivityPo registerActivityPo, HttpServletRequest request) {
        PcUserInfo userInfo = this.userInfoUtil.getPcUserInfo(request);
        registerActivityPo.setCreateUserId(userInfo.getAdminId());
        registerActivityPo.setOrgId(userInfo.getOrgId());
        this.registerActivityService.insertRegisterActivity(registerActivityPo);
    }

    /**
     * @param registerActivityPo 活动实体
     * @param registerActivityPo 注册活动实体
     * @Description：添加注册活动
     * @DateTime：2020/12/17 17:18
     * @Params：
     * @Return：
     */
    @PostMapping("/update")
    public void updateRegisterActivity(@RequestBody RegisterActivityPo registerActivityPo, HttpServletRequest request) {
        PcUserInfo userInfo = this.userInfoUtil.getPcUserInfo(request);
        registerActivityPo.setCreateUserId(userInfo.getAdminId());
        registerActivityPo.setOrgId(userInfo.getOrgId());
        this.registerActivityService.updateRegisterActivity(registerActivityPo);
    }

    /**
     * @param registerActivityPo 注册活动实体
     * @Description：编辑状态
     * @DateTime：2020/12/17 17:23
     * @Params：
     * @Return：
     */
    @PostMapping("/updateStatus")
    public void updateRegisterActivityStatus(@RequestBody RegisterActivityPo registerActivityPo, HttpServletRequest request) {
        this.userInfoUtil.getPcUserInfo(request);
        this.registerActivityService.updateRegisterActivityStatus(registerActivityPo.getActivityId(), registerActivityPo.getStatus());
    }

    /**
     * @param registerActivityPo 注册活动实体
     * @Description：删除注册活动
     * @DateTime：2020/12/17 17:25
     * @Params：
     * @Return：
     */
    @PostMapping("/delete")
    public void deleteRegisterActivity(@RequestBody RegisterActivityPo registerActivityPo, HttpServletRequest request) {
        this.userInfoUtil.getPcUserInfo(request);
        this.registerActivityService.deleteRegisterActivity(registerActivityPo.getActivityId());
    }
}
